from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse
from web.form.project import projectModelForm
from django.http import HttpResponse
from web import models
from utils.tencent.cos import create_bucket
import time
from django.conf import settings

# from django.views.decorators.csrf import csrf_exempt
#
#
# @csrf_exempt
def project_list(request):
    if request.method == "GET":
        """查看项目列表"""
        project_dict = {'star': [], 'my': [], 'join': []}
        # 我创建的
        my_project_list = models.Project.objects.filter(creator=request.tracer.user)
        for row in my_project_list:
            if row.star:
                project_dict['star'].append({"value": row, 'type': 'my'})
            else:
                project_dict['my'].append(row)
        # 我参与的
        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)
        for item in join_project_list:
            if item.star:
                project_dict['star'].append({"value": item.project, 'type': 'join'})
            else:
                project_dict['join'].append(item.project)

        form = projectModelForm(request)
        return render(request, 'web/project_list.html', {'form': form, 'project_dict': project_dict})
    form = projectModelForm(request, data=request.POST)
    if form.is_valid():
        # 为项目创建一个桶
        bucket = "{}-{}".format(request.tracer.user.phone, settings.TENCENT_COS_APPID)
        region='ap-chengdu'
        create_bucket(bucket,region)

        # 把桶和区域写到数据库
        form.instance.bucket = bucket
        form.instance.region = region

        form.instance.creator = request.tracer.user
        instance = form.save()
        # 3,项目初始化问题类型
        issues_type_object_list = []
        for item in models.IssuesType.PROJECT_INIT_LIST: #["任务", '功能', 'Bug']
            issues_type_object_list.append(models.IssuesType(project=instance,title=item))
        models.IssuesType.objects.bulk_create(issues_type_object_list)
        return JsonResponse({'status': True})
        # 创建项目
    return JsonResponse({'status': False, 'error': form.errors})


def project_star(request, project_type, project_id):
    """星标项目"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('web_project_list')
    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=True)
        return redirect('web_project_list')
    return HttpResponse("请求错误")


def project_unstar(request, project_type, project_id):
    """取消星标"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('web_project_list')
    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        return redirect('web_project_list')
    return HttpResponse("请求错误")
